# Maintainer: Alexey Pavlov <alexpux@gmail.com>

if [[ "${MINGW_PREFIX}" =~ /clang.* ]] ; then
    # _toolset=clang creates dlls names without the `lib' prefix
    _toolset=gcc
else
    _toolset=gcc
fi

_realname=boost
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.78.0
_boostver=${pkgver//./_}
pkgrel=1
pkgdesc="Free peer-reviewed portable C++ source libraries (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://www.boost.org/"
license=('custom')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-bzip2"
         "${MINGW_PACKAGE_PREFIX}-icu"
         "${MINGW_PACKAGE_PREFIX}-xz"
         "${MINGW_PACKAGE_PREFIX}-zstd"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "lndir")
options=('strip' 'buildflags' 'staticlibs')
source=(https://boostorg.jfrog.io/artifactory/main/release/${pkgver}/source/boost_${_boostver}.tar.bz2
        boost-1.58.0-pool.patch
        boost-1.63.0-python-test-PyImport_AppendInittab.patch
        boost-1.57.0-build-optflags.patch
        boost-1.68.0-pool-test_linking.patch
        boost-1.57.0-python-abi_letters.patch
        boost-1.57.0-python-libpython_dep.patch
        boost-1.69-old-style-names-layout.patch
        boost-1.70.0-fix-python-install.patch
        boost-1.73.0-fix-locale-icu.patch
	boost-1.78.0-fix-install-fiber-and-stacktrace.patch
        using-mingw-w64-python.patch
        msys2-mingw-folders-bootstrap.patch)
sha256sums=('8681f175d4bdb26c52222665793eef08490d7758529330f98d3b29dd0735bccc'
            '4551ba9edf64c8ccdab4f4890c20e2e8cf6d4ffa1169d251df11e30d25b886b8'
            'b22196b6415f5e1c0fe56b49a12ea7c20073b15a5f31907f363c7be38d70d628'
            '95e794e586943a5280f1da10a8990e5b639afcda140bac93d74c4f5f57855286'
            '9d159f713fecbcb2b8faf2534da4cbb223bde6c3e393218aca2c0b3dfc3374c1'
            'cf46e9b791b45937d5af26b0f35dcdfa416cd9b62ef6c29e3064464835634628'
            '01758929643f92530512230d37df9793e6481cd6ce6310e3a79cee5ba287858c'
            '0270b8a647ccf5deeae440e4f9eef99ccea4fdc8694758810ae608140d4ded9a'
            '5e5fc9e04482903e1a0e7c6c4e81b57448b8dc86c707d1436d69ddc5010219ff'
            '5ef2b1011dc960b7f55d4da89bcd75418eb696dce677b9fd0c78632f4109e7d3'
            '1801fa7eb557acd45e180109fa998dff7486a4d74e7f1742a6a056fef9849415'
            '5c38e08ba63695afa79a29d5d3ea22ef1ecf79f91d48ae922e1a489e83782742'
            '5117629ee577de0da800b6923675683ba69422cdbe958e70c9081fdc6886402e')

prepare() {
  cd "${srcdir}/boost_${_boostver}"

  # https://bugzilla.redhat.com/show_bug.cgi?id=828856
  # https://bugzilla.redhat.com/show_bug.cgi?id=828857
  # https://svn.boost.org/trac/boost/ticket/6701
  msg2 "boost-1.58.0-pool.patch"
  patch -p0 -i ${srcdir}/boost-1.58.0-pool.patch

  # https://svn.boost.org/trac/boost/ticket/9038
  msg2 "boost-1.68.0-pool-test_linking.patch"
  patch -p1 -i ${srcdir}/boost-1.68.0-pool-test_linking.patch

  # https://bugzilla.redhat.com/show_bug.cgi?id=1102667
  msg2 "boost-1.57.0-python-libpython_dep.patch"
  patch -p1 -i ${srcdir}/boost-1.57.0-python-libpython_dep.patch
  msg2 "boost-1.57.0-python-abi_letters.patch"
  patch -p1 -i ${srcdir}/boost-1.57.0-python-abi_letters.patch
  msg2 "boost-1.60.3-python-test-PyImport_AppendInittab.patch"
  patch -p1 -i ${srcdir}/boost-1.63.0-python-test-PyImport_AppendInittab.patch

  # https://bugzilla.redhat.com/show_bug.cgi?id=1190039
  msg2 "boost-1.57.0-build-optflags.patch"
  patch -p1 -i ${srcdir}/boost-1.57.0-build-optflags.patch

  # Back old names for libraries (without architecture)
  msg2 "boost-1.69-old-style-names-layout.patch"
  patch -p1 -i ${srcdir}/boost-1.69-old-style-names-layout.patch

  # Use our own build Python
  msg2 "using-mingw-w64-python.patch"
  patch -p1 -i ${srcdir}/using-mingw-w64-python.patch

  msg2 "msys2-mingw-folders-bootstrap.patch"
  patch -p1 -i ${srcdir}/msys2-mingw-folders-bootstrap.patch
  
  # Fix installation of both Python versions
  msg2 "boost-1.70.0-fix-python-install.patch"
  patch -p1 -i ${srcdir}/boost-1.70.0-fix-python-install.patch
  
  # Fix Boost::locale compilation to use ICU backend
  msg2 "boost-1.73.0-fix-locale-icu.patch"
  patch -p1 -i ${srcdir}/boost-1.73.0-fix-locale-icu.patch

  # Fix install of fiber and stacktrace
  msg2 "boost-1.78.0-fix-install-fiber-and-stacktrace.patch"
  patch -p1 -i ${srcdir}/boost-1.78.0-fix-install-fiber-and-stacktrace.patch
}

setb2args() {
    case ${CARCH} in
	i686|armv7)
            local _model=32
            ;;
	x86_64|aarch64)
            local _model=64
            ;;
    esac

    if check_option "debug" "y"; then
        local variant=debug
    else
        local variant=release
    fi

    b2args="address-model=${_model} \
    link=shared,static \
    pch=off \
    runtime-link=shared \
    threading=multi \
    threadapi=win32 \
    toolset=$_toolset \
    variant=${variant} \
    python=${_pyver} \
    --debug-configuration \
    --prefix=${pkgdir}${MINGW_PREFIX} \
    --layout=tagged \
    --no-cmake-config \
    -sHAVE_ICU=1 \
    -sICU_LINK=\"-L${MINGW_PREFIX}/lib -licuuc -licuin -licudt\" \
    -sICU_PATH=${MINGW_PREFIX} \
    -sICU_ICUUC_NAME=icuuc \
    -sICU_ICUDT_NAME=icudt \
    -sICU_ICUIN_NAME=icuin \
    -sICONV_PATH=${MINGW_PREFIX} \
    -sICONV_LINK=\"-L${MINGW_PREFIX}/lib -liconv\" \
    -sNO_BZIP2 \
    -sBZIP2_BINARY=bz2 \
    -sBZIP2_INCLUDE=${MINGW_PREFIX}/include \
    -sBZIP2_LIBPATH=${MINGW_PREFIX}/lib \
    -sNO_ZLIB \
    -sZLIB_BINARY=z \
    -sZLIB_INCLUDE=${MINGW_PREFIX}/include \
    -sZLIB_LIBPATH=${MINGW_PREFIX}/lib \
    -d2 \
    -q"
}

build() {
  local PREFIX_DEPS=$(cygpath -am ${MINGW_PREFIX})

  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  mkdir -p "${srcdir}/build-${MINGW_CHOST}"
  lndir "${srcdir}/boost_${_boostver}" "${srcdir}/build-${MINGW_CHOST}"

  cd "${srcdir}/build-${MINGW_CHOST}"

  local _pyver=$(${MINGW_PREFIX}/bin/python -c "import sys;sys.stdout.write('.'.join(map(str, sys.version_info[:2])))")

  echo "using python : ${_pyver} : ${PREFIX_DEPS}/bin/python.exe : ${PREFIX_DEPS}/include/python${_pyver} : ${PREFIX_DEPS}/lib/python${_pyver}/config-${_pyver} ;" >> ./tools/build/src/user-config.jam
  #echo "using gcc : : ${PREFIX_DEPS}/bin/g++.exe : <rc>${PREFIX_DEPS}/bin/windres.exe ;" >> ./tools/build/src/user-config.jam

  ./bootstrap.sh --with-icu=${MINGW_PREFIX} --with-python=${MINGW_PREFIX} --with-toolset=$_toolset

  setb2args
  ./b2 $b2args -j$(nproc)
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"

  local _pyver=$(${MINGW_PREFIX}/bin/python -c "import sys;sys.stdout.write('.'.join(map(str, sys.version_info[:2])))")

  setb2args
  ./b2 $b2args install

  mkdir -p ${pkgdir}${MINGW_PREFIX}/bin
  mv ${pkgdir}${MINGW_PREFIX}/lib/*.dll ${pkgdir}${MINGW_PREFIX}/bin
  
  # install b2 and bjam
  install -Dm755 b2.exe "${pkgdir}${MINGW_PREFIX}"/bin/b2.exe
  cp -rf "${pkgdir}${MINGW_PREFIX}"/bin/b2.exe "${pkgdir}${MINGW_PREFIX}"/bin/bjam.exe
}
